package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.mapper.RegionMapper;
import cn.wolfcode.luowowo.article.query.RegionQuery;
import cn.wolfcode.luowowo.article.service.IRegionService;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class RegionServiceImpl implements IRegionService {

    @Autowired
    private RegionMapper regionMapper;

    @Override
    public PageInfo<Region> listAllByPage(RegionQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), "sequence");
        return new PageInfo<>(regionMapper.selectAll());
    }

    @Override
    public void saveOrUpdate(Region region) {

        AssertUtil.notNull(region.getName(),"区域名称不能为空");
        if (region.getId() == null) {
            regionMapper.insert(region);
        } else {
            regionMapper.updateByPrimaryKey(region);
        }
    }

    @Override
    public Region getRegionById(Long rid) {
        return regionMapper.selectByPrimaryKey(rid);
    }

    @Override
    public void deleteById(Long id) {
         regionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void changeHotValue(Long id, boolean hot) {
        regionMapper.updateByhot(id, hot);
    }

    @Override
    public List<Destination> listHotRegion() {
        return regionMapper.selectHotRegion();
    }


}
